import 'dart:io';
import 'package:eh_mobile/common/apis/network/network.dart';
import 'package:eh_mobile/common/models/index.dart';
import 'package:eh_mobile/common/store/index.dart';
import 'package:logger/logger.dart';
import '../store/config_store.dart';
import 'api_path.dart';

// 首页网络请求
class HomepageAPI {
// 创建新的移动设备
  static Future<dynamic> mobileNew(Map<String, dynamic> mobileIdentity) async {
    // 组装网络请求基础数据
    Map<String, dynamic> netUserInfoDict = {
      'type': Platform.operatingSystem,
      'version': ConfigStore().appVerson,
      'name': ConfigStore().deviceName,
    };
    var response = await Network().post(api_mobile_new, param: netUserInfoDict);

    if (response.data['identity'] != null) {
      UserStore().saveMobileIdentity(response.data['identity']);
    }
    if (response.data['token'] != null) {
      UserStore().saveMobileToken(response.data['token']);
    }

    return response;
  }

  // 房间列表
  static Future<List<RoomListModel>> getRoomList() async {
    var response = await Network().post(api_room_list, param: {});
    if (response.data['result'] != null) {
      List resultList = response.data['result'];
      List modelList = <RoomListModel>[];
      for (var v in resultList) {
        modelList.add(RoomListModel.fromJson(v));
      }
    }
    return [];
  }

  // 房间区域列表
  static Future<List<RegionListModel>> getRegionList() async {
    var response = await Network().post(api_region_list, param: {});
    List<RegionListModel> modelList = [];
    if (response.data['result'] != null) {
      List resultList = response.data['result'];
      for (var v in resultList) {
        modelList.add(RegionListModel.fromJson(v));
      }
    }
    return modelList;
  }

  // 获取卡片配置
  static Future<List<RoomCardModel>> getRoomCards(int id) async {
    var response = await Network().post(api_room_get_cards, param: {'id': id});
    List<RoomCardModel> modelList = [];
    if (response.data['result'] != null) {
      List resultList = response.data['result'];
      for (var v in resultList) {
        modelList.add(RoomCardModel.fromJson(v));
      }
    }
    return modelList;
  }

  //激活配对码
  static Future<Map> getActivePairCode(String codeStr) async {
    var response = await Network().post(api_mobile_activepaircode,
        param: {'code': codeStr},
        headers: {'mobile-token': UserStore().mobileToken});
    Logger().d('绑定接口返回的数据:$response');
    Map map = {};
    if (response is Map) {
      if (response['statusCode'] != 200) {
        map = response;
      }
      return map;
    }
    if (response.data != null) {
      map = response.data;
      return map;
    }
    return map;
  }

  //查询配对列表
  static Future<List> getMobilePairInfo() async {
    var response = await Network().post('/mobile/pairinfo',
        headers: {'mobile-token': UserStore().mobileToken});
    List list = [];
    if (response != null && response.data != null) {
      list = response.data;
    }
    return list;
  }

  //创建数据通道
  static Future<Map> createDataChannel(String systemIdentity) async {
    var response = await Network().post('/mobile/createchannel',
        param: {'system': systemIdentity},
        headers: {'mobile-token': UserStore().mobileToken});
    Logger().d('创建数据通道接口返回的数据:$response');
    Map map = {};
    if (response is Map) {
      if (response['statusCode'] != 200) {
        map = response;
      }
      return map;
    }
    if (response.data != null) {
      map = response.data;
      return map;
    }
    return map;
  }
}
